<?php
// 引入请求网址
require_once("request.php");
// 要使用storage
use sinacloud\sae\Storage as Storage;

define("APPID","wx9bdc780b9673cf2e");
define("SECRET","f5a8570a60a826631e255178fa729411");
// 获取新的access_token的函数
function getNewAccess_TokenInfo() {
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
  $response = httpGet($url);
  return $response;

}
echo getAccessToken();
// 获取access_token的函数
function getAccessToken() {
  // 让$responseObject等于storage中access_token的返回值
  $responseObject = readAccessTokenInfoFromStorage();
  if ($responseObject == false) {
    // 如果没有，就获取access_token  其实是获取到json串
    $res = getNewAccess_TokenInfo();
    // 解析json串
    $obj = json_decode($res);
    // 获取access_token和expries_in
    $access_token = $obj->access_token;
    $expires_in = $obj->expires_in;
    // 先获取当前的时间 1970.1.1  00:00:00 到现在有多少秒
    $current_time = time();
    // 得到过期的时间点
    $expires_time = $current_time + $expires_in - 200;
    // 存储到
    // 创建一个空的数组的对象 (强行转换为对象)
    $saveObj = (object)array();
    // 将我们要存储的东西存入
    $saveObj->access_token = $access_token;
    $saveObj->expires_time = $expires_time;
    // 转换成json串
    $saveString = json_encode($saveObj);
    // 创建一个storage对象
    $s = new Storage();
    // 获取bucket
     $bucket = $s->getBucket("bucket");
    //  如果bucket不存在就创建bucket
     if ($bucket == false) {
       $s->putBucket("bucket",".r:*");
     }
    //  写入操作
    $s->putObject($saveString,"bucket","access_token.txt",array(),array("Content-Type"=>"text/plain;charset=utf-8"));
    return $access_token;
  } else {
    // 如果存有便获取access_token 和 expires_in
    $access_token = $responseObject->access_token;
    $expires_time = $responseObject->expires_time;
    // 获取当前的时间 
    $current_time = time();
    // 判断是否过期
    if ($current_time>=$expires_time) {
      // 如果已经过期了 重新获取
      $res = getNewAccess_TokenInfo();
      $obj = json_decode($res);
      // 获取access_token和expires_in
      $access_token = $obj->access_token;
      $expires_in = $obj->expires_in;
      $expires_time = $current_time + $expries_in - 200;
      // 写入
      $saveObj = (object)array();
      $saveObj->access_token = $access_token;
      $saveObj->expires_time = $expires_time;
      // 将对象转换成json串
      $saveString = json_encode($saveObj);
      // 写入bucket
      $s = new Storage();
      // 先获取bucket
      $bucket = $s->getBucket("bucket");
      if ($bucket == false) {
        $s->putBucket("bucket",".r:*");
      }
      // 写入
      $s->putObject($saveString,"bucket","access_token.txt",array(),array("Content-Type"=>"text/plain;charset=utf-8"));
      return $access_token;
    } else {
      return $access_token;
    }
  }
}
function readAccessTokenInfoFromStorage(){
  // 先判断bucket中有没有access_token
  // 如果存储了并且没有过期 直接使用
  // 如果过期了就重新获取并存储
$s = new storage();
// 获取 bucket  @符号取消用户警告
$bucket = @$s->getBucket("bucket");
if ($bucket == false) {
  return false;
} else {
  $object = $s->getObject("bucket","access_token.txt");
  // 将json转换为$object返回 读取出的文件中有几个部分，其中内容部分中的下标为body
  return json_decode($object->body);
}
}


createMenu();
// 创建自定义菜单
function createMenu() {
  $access_token = getAccessToken();
  // 将access_token传入到网址中
  $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
  // post请求所需的参数
  $postdata = '{
    "button":[
      {
        "name":"点击事件",
        "sub_button":[
          {
            "type":"click",
            "name":"每日一文",
            "key":"sendText"
          },{
            "type":"click",
            "name":"每日一图",
            "key":"sendImage"
          },{
            "type":"click",
            "name":"每日一段",
            "key":"sendVoice"
          },{
            "type":"click",
            "name":"每日一片",
            "key":"sendVideo"
          }
        ]
      },{
        "name":"扫码和相册",
        "sub_button":[
          {
            "type":"scancode_push",
            "name":"扫码推事件",
            "key":"scancode"
          },{
            "type":"scancode_waitmsg",
            "name":"扫码带提示",
            "key":"scancode"
          },{
            "type":"pic_sysphoto",
            "name":"系统拍照发图",
            "key":"photo"
          },{
            "type":"pic_photo_or_album",
            "name":"拍照或相册发图",
            "key":"rselfmenu"
          },{
            "type":"pic_weixin",
            "name":"微信相册发图",
            "key":"rselfmenu"
          }
        ]
      },{
        "name":"其他",
        "sub_button":[
          {
            "type":"view",
            "name":"更多欢乐",
            "url":"http://1.happyer.applinzi.com/phone_one.html",
          },{
            "name":"我在这儿",
            "type":"location_select",
            "key":"location_select"
          }
        ]
      }
    ]
  }';
  // 得到返回值
  $response = httpPost($url,$postdata);
  return $response;
}




 ?>
